/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAL;

import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author Mr Bao
 */
public class DAL {

    private EntityManager em;
    private EntityManagerFactory emf;

    public DAL() {
        emf = Persistence.createEntityManagerFactory("UpAnh");
        em = emf.createEntityManager();
    }

    public List<Album> GetAllAlbumByUserId(int userId) {
        Query query = em.createQuery("SELECT a FROM Album a WHERE a.userId = " + userId);
        return (List<Album>) query.getResultList();
    }

    public List<Imageupload> GetAllImageuploads(int albumId) {
        Query query = em.createQuery("SELECT i FROM Imageupload i WHERE i.albumId = " + albumId);
        return (List<Imageupload>) query.getResultList();
    }

    public boolean Insert(Serializable row) {
        try {
            em.getTransaction().begin();
            em.persist(row);
            em.getTransaction().commit();
            return true;
        } catch (Exception e) {
            System.out.println(e);
            return false;
        }
    }

    public boolean Update(Serializable row) {
        try {
            em.getTransaction().begin();
            em.merge(row);
            em.getTransaction().commit();
            return true;
        } catch (Exception e) {
            System.out.println(e);
            return false;
        }
    }

    public boolean Remove(Serializable row) {
        try {
            em.getTransaction().begin();
            em.remove(row);
            em.getTransaction().commit();
            return true;
        } catch (Exception e) {
            System.out.println(e);
            return false;
        }
    }

    public Object GetRowByPrimaryKey(Class entity, int id) {
        return em.find(entity, id);
    }

    public List GetTable(String sql) {
        Query query = em.createQuery(sql);
        return query.getResultList();
    }

    public boolean CheckUser(String username) {
        boolean isExist = false;
        try {
            Query query = em.createQuery("Select a from Account a where a.username='" + username + "'");
            if (query.getResultList().size() > 0) {
                isExist = true;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return isExist;
    }

    public Account Login(String username, String password) {
        try {
            Query query = em.createQuery("Select a from Account a where a.username='" + username + "' and a.password='" + password + "'");
            if (query.getResultList().size() > 0) {
                Account account = (Account) query.getSingleResult();
                return account;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public boolean Register(String username, String password) {
        if (CheckUser(username)) {
            return false;
        }
        Account account = new Account();
        account.setUsername(username);
        account.setPassword(password);
        return Insert(account);
    }

    public Account GetAccountByUserId(int userId) {
        Query query = em.createQuery("Select a from Account a where a.id=" + userId);
        return (Account) query.getSingleResult();
    }

    public int GetID() {
        Query query = em.createQuery("SELECT i FROM Imageupload i ORDER BY i.id asc");
        if (query!=null && query.getResultList().size()>0){
            return ((Imageupload)query.getResultList().get(query.getResultList().size()-1)).getId();
        }
        return 1;
    }

    public List<Imageupload> GetNewImageUpload() {
        Query query = em.createQuery("SELECT i FROM Imageupload i ORDER BY i.dateUpload DESC");
        return (List<Imageupload>) query.getResultList();
    }
}
